package com.qyhl.webtv.module_circle.circle.complain;

import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.gyf.immersionbar.ImmersionBar;
import com.qyhl.webtv.basiclib.base.BaseActivity;
import com.qyhl.webtv.basiclib.base.BaseIViewPresenter;
import com.qyhl.webtv.basiclib.utils.StringUtils;
import com.qyhl.webtv.basiclib.utils.dialog.LoadingDialog;
import com.qyhl.webtv.commonlib.constant.ARouterPathConstant;
import com.qyhl.webtv.module_circle.R;
import com.qyhl.webtv.module_circle.R2;
import com.umeng.analytics.MobclickAgent;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import es.dmoral.toasty.Toasty;

/**
 * Created by qyhl on 2018/1/24.
 * 圈子举报
 */

@Route(path = ARouterPathConstant.CIRCLE_COMPLAIN)
public class CircleComplainActivity extends BaseActivity implements CircleComplainContract.ComplainView {
    @BindView(R2.id.title)
    TextView title;
    @BindView(R2.id.content)
    EditText content;
    @BindView(R2.id.commit)
    TextView commit;
    @BindView(R2.id.btn_rumour)
    CheckBox btnRumour;
    @BindView(R2.id.btn_illegal)
    CheckBox btnIllegal;
    @BindView(R2.id.btn_violence)
    CheckBox btnViolence;
    @BindView(R2.id.btn_pornographic)
    CheckBox btnPornographic;
    @BindView(R2.id.btn_adv)
    CheckBox btnAdv;
    @BindView(R2.id.btn_other)
    CheckBox btnOther;

    private CircleComplainPresenter mPresenter;

    @Autowired(name = "id")
    String topicId = "";
    @Autowired(name="username")
    String dstUserName = "";
    @Autowired(name="type")
    String type = "";//1圈子 2评论 10用户
    private String summary;

    private LoadingDialog.Builder builder;
    private List<CheckOption> mDataList;

    private StringBuffer option;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            Window window = getWindow();
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.WHITE);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }

    }

    @Override
    protected int getContentView() {
        return R.layout.circle_activity_circle_complain;
    }

    @Override
    protected void setImmersionBar(ImmersionBar mImmersionBar) {
        setDefaultImmerBar();
    }

    @Override
    protected BaseIViewPresenter initBaseIViewPresenter() {
        return null;
    }

    @Override
    protected void init() {
        mPresenter = new CircleComplainPresenter(this);
        initView();
    }

    @Override
    protected void setListener() {
        setListeners();
    }

    /**
     * 初始化
     */
    private void initView() {
        title.setText("举报");

        mDataList = new ArrayList<>();

        mDataList.add(new CheckOption(false, "造谣传谣"));
        mDataList.add(new CheckOption(false, "违法违规"));
        mDataList.add(new CheckOption(false, "血腥暴力"));
        mDataList.add(new CheckOption(false, "涉黄信息"));
        mDataList.add(new CheckOption(false, "垃圾广告"));
        mDataList.add(new CheckOption(false, "其它"));


        builder = new LoadingDialog.Builder(CircleComplainActivity.this);
        builder.setTitle("提交中...");
        builder.setCanceledOnTouchOutside(false);
        builder.setCancelable(true);
    }

    /**
     * 设置监听
     */
    private void setListeners() {
        //提交
        commit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                option = new StringBuffer();
                for (int i = 0; i < mDataList.size(); i++) {
                    if (mDataList.get(i).isCheck()) {
                        option.append(mDataList.get(i).getName() + ",");
                    }
                }


                if (StringUtils.isNotEmpty(option.toString())) {
                    if (StringUtils.isNotEmpty(content.getText().toString())) {
                        summary = option.append(content.getText().toString()).toString();
                    } else {
                        summary = option.toString();
                    }
                    builder.show();
                    mPresenter.doComplain(topicId, dstUserName, type, summary);
                } else {
                    Toasty.normal(CircleComplainActivity.this, "请选择举报类型！").show();
                }
            }
        });

        //造谣
        btnRumour.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    mDataList.get(0).setCheck(true);
                } else {
                    mDataList.get(0).setCheck(false);
                }
            }
        });

        //违法
        btnIllegal.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (isChecked) {
                mDataList.get(1).setCheck(true);
            } else {
                mDataList.get(1).setCheck(false);
            }
        });

        //暴力
        btnViolence.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (isChecked) {
                mDataList.get(2).setCheck(true);
            } else {
                mDataList.get(2).setCheck(false);
            }
        });

        //色情
        btnPornographic.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (isChecked) {
                mDataList.get(3).setCheck(true);
            } else {
                mDataList.get(3).setCheck(false);
            }
        });

        //广告
        btnAdv.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (isChecked) {
                mDataList.get(4).setCheck(true);
            } else {
                mDataList.get(4).setCheck(false);
            }
        });

        //其他
        btnOther.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (isChecked) {
                mDataList.get(5).setCheck(true);
            } else {
                mDataList.get(5).setCheck(false);
            }
        });

    }

    /**
     * 举报成功
     *
     * @param msg
     */
    @Override
    public void complainSuccess(String msg) {
        builder.dismiss();
        Toasty.normal(this, msg).show();
        finish();
    }

    /**
     * 举报失败
     *
     * @param msg
     */
    @Override
    public void complainError(String msg) {
        builder.dismiss();
        Toasty.normal(this, msg).show();
    }

    @Override
    protected void onResume() {
        super.onResume();
        //baidu
        //umeng
        MobclickAgent.onPageStart("圈子举报");
        MobclickAgent.onResume(this);
    }


    @Override
    protected void onPause() {
        super.onPause();
        //baidu
        //umeng
        MobclickAgent.onPageEnd("圈子举报");
        MobclickAgent.onPause(this);
    }

    public class CheckOption implements Serializable {
        private boolean isCheck;
        private String name;

        CheckOption(boolean isCheck, String name) {
            this.isCheck = isCheck;
            this.name = name;
        }

        public boolean isCheck() {
            return isCheck;
        }

        public void setCheck(boolean check) {
            isCheck = check;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }
}
